'use strict';

var resp = module.exports = {};
var uuid = require('uuid'),
  config = require('./config'),
  errCode = require('../config/my_err_code'),
  url = 'https://www.yozh.tk',
  errorUrl = url + '/v1/errors/';

resp.createSimpleData = function(module, id) {
  var obj = {};
  obj.type = module;
  obj.id = id;
  return [obj];
};

resp.createData = function(module, id, entity) {
  var obj = {};
  obj.type = module;
  obj.id = id;
  if (typeof(entity) === 'object') {
    obj.attributes = entity;
  }
  return obj;
};

resp.createSimpleLinks = function(self, next, last) {
  var obj = {};
  if (self) {
    obj.self = url + self;
  }
  if (next) {
    obj.next = url + next;
  }
  if (last) {
    obj.last = url + last;
  }
  return obj;
};

resp.createLinks = function(version, module, self) {
  version = version || '';
  module = module + 's' || '';
  var prefix = url + version + module;
  var obj = {};
  if (self) {
    obj.self = prefix + self;
  }
  return obj;
};

resp.createErrors = function(status, code, detail, source, meta) {
  var obj = {};
  obj.id = uuid.v4();
  obj.links = errorUrl + obj.id;
  obj.status = status;
  obj.code = code;
  obj.title = errCode[code];
  obj.detail = detail;
  obj.source = source;
  if (typeof(meta) === 'object') {
    obj.meta = meta;
  }
  return obj;
};

resp.createResp = function(links, data, errors) {
  var obj = {};
  if (links) {
    obj.links = links;
  }
  if (data) {
    obj.data = data;
  }
  if (errors) {
    obj.errors = errors;
  }
  return obj;
};
